package gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import operations.Lifo;

import languages.Language;

import components.Coordinate;
import components.Node;

public class DialogEditPoint extends JDialog implements ActionListener{

	private Node node;
	private Window parent;
	private Language lang;
	
	private JTextField lattitude,longtitude;
	private boolean degrees;
	private JCheckBox type;
	
	public DialogEditPoint(Window parent, Language lang, Node node) {
		super(parent, lang.dialogEditPointTitle(), true);
		
		this.lang = lang;
		this.node = node;
		this.parent = parent;
		this.degrees = true;
		
		this.getContentPane().add(createGUI());
	}

	private JPanel createGUI() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4,2));
		
		panel.add(new JLabel(lang.dialogEditPointLat()));
		lattitude = new JTextField(""+node.getLattitude());
		panel.add(lattitude);
		
		panel.add(new JLabel(lang.dialogEditPointLon()));
		longtitude = new JTextField(""+node.getLongtitude());
		panel.add(longtitude);
		
		panel.add(new JLabel(lang.dialogEditPointType()));
		type = new JCheckBox();
		type.setSelected(false);
		type.addActionListener(this);
		type.setActionCommand("CHANGE");
		panel.add(type);
		
		JButton button = new JButton(lang.tiChange());
		button.addActionListener(this);
		button.setActionCommand("ADD");
		panel.add(button);

		button = new JButton(lang.tiClear());
		button.addActionListener(this);
		button.setActionCommand("CLEAR");
		panel.add(button);
		
		return panel;
	}
	
	private void saveModyfication(){
		if(degrees){
			node.setLattitude(Float.parseFloat(lattitude.getText()));
			node.setLongtitude(Float.parseFloat(longtitude.getText()));
		}
		else{
			node.setLattitude(Coordinate.milisecondsToDegrees(Integer.parseInt(lattitude.getText())));
			node.setLongtitude(Coordinate.milisecondsToDegrees(Integer.parseInt(longtitude.getText())));
		}
		
		
		parent.getNodeLayer().changeSelectedGraphic(node);
		parent.getSW().addNewEvent(new Lifo(node, Lifo.editNode));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		if("CHANGE".equals(command)){
			if(this.type.isSelected()){
				if(degrees){
					changeTypeToMiliseconds();
				}
			}
			else{
				if(!degrees){
					changeTypeToDerees();
				}
			}
		}
		else if("ADD".equals(command)){
			saveModyfication();
		}
		else if("CLEAR".equals(command)){
			clearFields();
		}
	}

	private void clearFields() {
		this.lattitude.setText(""+node.getLattitude());
		this.longtitude.setText(""+node.getLongtitude());
		this.degrees=true;
		this.type.setSelected(false);
	}

	private void changeTypeToMiliseconds() {
		float d = Float.parseFloat(this.longtitude.getText());
		int i = (int)d*3600*1000;

		this.longtitude.setText(""+i);
		
		d = Float.parseFloat(this.lattitude.getText());
		i = (int)d*3600*1000;

		this.lattitude.setText(""+i);
		
		degrees = false;
	}
	
	private void changeTypeToDerees() {
		int d = Integer.parseInt(this.longtitude.getText());
		float i = d/3600;
		i /= 1000;

		this.longtitude.setText(""+i);
		
		d = Integer.parseInt(this.longtitude.getText());
		i = d/3600;
		i /= 1000;

		this.lattitude.setText(""+i);
		
		degrees = true;
	}

	
}
